package com.momoauth.api_client.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.momoauth.api_client.entity.User_Role;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface UserRoleMapper extends BaseMapper<User_Role> {

    /**
     * 查询是否有用户使用该角色
     * @param role_id
     * @return
     */
    @Select("SELECT 1 FROM user_role WHERE role_id = #{role_id} LIMIT 1")
    Integer existUserRole(Long role_id);

    /**
     * 用户角色列表
     * @param user_id
     * @return
     */
    @Select("SELECT role_Id,roleName FROM user_role INNER JOIN role ON roleId = role_id WHERE user_id = #{user_id}")
    List<User_Role> getUserRoleList(Long user_id);

    /**
     * 统计用户角色
     * @param user_id
     * @param role_id
     * @return
     */
    @Select("SELECT COUNT(1) FROM user_role WHERE user_id = #{user_id} AND role_id = #{role_id}")
    Long countUserRole(@Param("user_id") Long user_id, @Param("role_id") Long role_id);

    /**
     * 删除所有用户角色
     * @param user_id
     * @return
     */
    @Update("DELETE FROM user_role WHERE user_id = #{user_id}")
    Long deleteAllUserRole(@Param("user_id") Long user_id);
}
